/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.comm.io;

import blueprint4j.utils.*;

import java.io.*;

/**
 * This class Decodes from Base64 
 */
class LineReaderRead extends LineReader {
	//private PushbackInputStream stream = null;
	private InputStream stream = null;
	private static final int MAX_READ = 20480;
	private byte [] unread = new byte[]{};
	
	//protected LineReaderRead(PushbackInputStream stream) {
	protected LineReaderRead(InputStream stream) {	
		super();
		this.stream = stream;
	}
	
	public void close() throws IOException {
		stream.close();
	}
	
	public byte[] getUnread() {
		return unread;
	}
	
	protected void unread(byte data[]) throws IOException {
		//stream.unread(data);
		unread = ByteM.concat(unread,data);
	}
	
	protected void unread(byte data[],int start,int len) throws IOException {
		//stream.unread(data,start,len);
		unread = ByteM.concat(unread,data,new Integer(start),new Integer(len));
	}

	protected boolean available() throws IOException {
		return stream.available() > 0 || unread.length > 0;
	}
	
	protected int availableInTopStream() throws IOException {
		return stream.available() + unread.length;
	}
	
	protected byte[] read() throws IOException,TimeoutException {
		int available = stream.available();
		byte buffer[] = new byte[available> MAX_READ?MAX_READ:available];
		stream.read(buffer);
		buffer = ByteM.concat(unread,buffer);
		unread = new byte[]{};
		
		return buffer;
	}
	
	public byte[] readBytes() throws IOException,TimeoutException {
		return read();
	}
}
